﻿Public Class ErrorRecetaDAO
    '@Accion as int,
    '@ErrorRecetaId as int,
    '@ErrorRecetaMotivo as text,
    '@ErrorRecetaCorreccion as text,
    '@ErrorRecetaCorregido as tinyint,
    '@EmpleadoId as int,
    '@TipoErrorRecetaId as int,
    '@RecetaId as int

    Public Shared Function insertarErrorReceta(ByVal ErrorRecetaMotivo As String, ByVal ErrorRecetaCorreccion As String, ByVal ErrorRecetaCorregido As Boolean, ByVal EmpleadoId As Integer, ByVal TipoErrorRecetaId As Integer, ByVal RecetaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_errorReceta"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@ErrorRecetaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@ErrorRecetaMotivo", ErrorRecetaMotivo).DbType = DbType.String
                    .AddWithValue("@ErrorRecetaCorreccion", ErrorRecetaCorreccion).DbType = DbType.String
                    .AddWithValue("@ErrorRecetaCorregido", ErrorRecetaCorregido).DbType = DbType.Boolean
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                    .AddWithValue("@TipoErrorRecetaId", TipoErrorRecetaId).DbType = DbType.Int32
                    .AddWithValue("@RecetaId", RecetaId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarErrorReceta(ByVal ErrorRecetaId As Integer, ByVal ErrorRecetaMotivo As String, ByVal ErrorRecetaCorreccion As String, ByVal ErrorRecetaCorregido As Boolean, ByVal EmpleadoId As Integer, ByVal TipoErrorRecetaId As Integer, ByVal RecetaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_errorReceta"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@ErrorRecetaId", ErrorRecetaId).DbType = DbType.Int32
                    .AddWithValue("@ErrorRecetaMotivo", ErrorRecetaMotivo).DbType = DbType.String
                    .AddWithValue("@ErrorRecetaCorreccion", ErrorRecetaCorreccion).DbType = DbType.String
                    .AddWithValue("@ErrorRecetaCorregido", ErrorRecetaCorregido).DbType = DbType.Boolean
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                    .AddWithValue("@TipoErrorRecetaId", TipoErrorRecetaId).DbType = DbType.Int32
                    .AddWithValue("@RecetaId", RecetaId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarErrorReceta(ByVal ErrorRecetaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_errorReceta"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@ErrorRecetaId", ErrorRecetaId).DbType = DbType.Int32
                    .AddWithValue("@ErrorRecetaMotivo", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@ErrorRecetaCorreccion", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@ErrorRecetaCorregido", Constante.MENOSUNO).DbType = DbType.Boolean
                    .AddWithValue("@EmpleadoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@TipoErrorRecetaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@RecetaId", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerErroresRecetas(ByVal ErrorRecetaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_errorReceta_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@ErrorRecetaId", ErrorRecetaId).DbType = DbType.Int32
            da.Fill(ds, "ErrorReceta")

            Return ds.Tables("ErrorReceta")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function grillaErrorReceta(ByVal RecetaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_errorReceta_grilla", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@RecetaId", RecetaId).DbType = DbType.Int32
            da.Fill(ds, "ErrorReceta")

            Return ds.Tables("ErrorReceta")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
End Class
